#ifndef __MGRAPH_H__
#define __MGRAPH_H__

#include <assert.h>

template<class DType>
struct MGraph {
	DType* data;
	int size;

	MGraph(int size);
	~MGraph() { delete[]data; };

	void addVertex(int v0, int v1);
	DType at(int row, int col);

	void dump();
};

template<class DType>
MGraph<DType>::MGraph(int size) {
	assert(size > 0);
	this->size = size;
	int size2 = size * size;
	data = new DType[size2];
	DType* d0 = data;
	assert(data);
	for (int i = 0; i < size2; i++)
		* d0++ = -1;
}

template<class DType>
void MGraph<DType>::addVertex(int v0, int v1) {
	assert(v0 != v1);
	assert(0 <= v0 && v0 < size);
	assert(0 <= v1 && v1 < size);

	data[v0 * size + v1] = 1;
	data[v1 * size + v0] = 1;
}

template<class DType>
DType MGraph<DType>::at(int row, int col) 
{
	assert(0 <= row && row < size);
	assert(0 <= col && col < size);

	return data[row * size + col];
}

template<class DType>
void MGraph<DType>::dump() 
{
	int j;
	assert(size > 0);
	DType* d0 = this->data;
	printf("%5s", "");
	for (j = 0; j < size; j++)
		printf("%4d", j);
	printf("\n -- \n");
	for (int i = 0; i < size; i++) {
		printf("%3d |", i);
		for (j = 0; j < size; j++)
			printf("%4d", *d0++);
		printf("\n");
	}
}

#endif //__MGRAPH_H__